package com.hst.capacity.infra.persistence.redis;

import cn.smartravel.map.baidu.RouteMatrix;
import cn.smartravel.map.baidu.response.RouteMatrixVO;
import cn.smartravel.map.qq.WeMapTemplate;
import cn.smartravel.map.qq.response.DirectionDrivingVO;
import cn.smartravel.map.qq.response.DistanceMatrixVO;
import cn.smartravel.map.qq.response.direction.RoutesBean;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hst.capacity.domain.common.constants.AliTripTransferConstants;
import com.hst.capacity.domain.common.constants.Constants;
import com.hst.capacity.domain.model.response.setting.RouteVO;
import com.hst.capacity.domain.repository.redis.RouteRedis;
import com.hst.capacity.infra.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@Repository
public class RouteRedisImpl implements RouteRedis {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private WeMapTemplate weMapTemplate;

    @Autowired
    private RouteMatrix routeMatrix;

    @Override
    public void writeRouteValue(String keySuffix, RouteVO vo) {
        String key = String.format(ROUTE_KEY, keySuffix);
        redisTemplate.opsForValue().set(key, JSON.toJSONString(vo), 600, TimeUnit.SECONDS);
    }

    @Override
    public RouteVO getRouteValue(String keySuffix) {
        String key = String.format(ROUTE_KEY, keySuffix);
        String RouteJson = redisTemplate.opsForValue().get(key);
        if (StringUtil.isBlank(RouteJson))
            return null;
        return JSONObject.parseObject(RouteJson, RouteVO.class);
    }

    @Override
    public RouteVO calOrderDistance(int coordinateType, String origins, String destinations) {
        //TODO 放在REDIS里面 KEY为origins+destinations
        String key = String.format("%s-%s", origins, destinations);
        log.info("calOrderDistance的key："+ key);
        RouteVO vo = this.getRouteValue(key);
        log.info("RouteVO"+ JSON.toJSONString(vo));
        if (vo == null) {
            vo = new RouteVO();
            if (coordinateType == AliTripTransferConstants.CoordinateType.get(Constants.GeoMapType.GCJ02)) {
                DistanceMatrixVO rm = weMapTemplate.matrix().matrixDriving(origins, destinations);
                //log.info("DistanceMatrixVO:"+ JSON.toJSONString(rm));
                if (rm == null || rm.getStatus() != 0)
                    return null;

                log.info("腾讯地图 DistanceMatrixVO:"+ JSON.toJSONString(rm));
                vo.setDistance(rm.getDistanceValue());
                vo.setSpendTime(rm.getDurationValue());
            } else {
                RouteMatrixVO rm = routeMatrix.getDriving(origins, destinations);
                if (rm == null || rm.getStatus() != 0)
                    return null;

                log.info("百度地图 RouteMatrixVO:"+ JSON.toJSONString(rm));
                vo.setDistance(rm.getDistanceValue());
                vo.setSpendTime(rm.getDurationValue());
            }
            this.writeRouteValue(key, vo);
        }

        return vo;
    }

    @Override
    public RouteVO calOrderDistance(int coordinateType, String origins, String destinations, HashMap<String, Object> reqParams) {
        //TODO 放在REDIS里面 KEY为origins+destinations
        String key = String.format("%s-%s", origins, destinations);
        log.info("calOrderDistance的key："+ key);
        RouteVO vo = this.getRouteValue(key);
        log.info("RouteVO"+ JSON.toJSONString(vo));
        if (vo == null) {
            vo = new RouteVO();
            if (coordinateType == AliTripTransferConstants.CoordinateType.get(Constants.GeoMapType.GCJ02)) {
                DirectionDrivingVO rm = weMapTemplate.direction().driving(origins, destinations, reqParams);
                //log.info("DistanceMatrixVO:"+ JSON.toJSONString(rm));
                if (rm == null || rm.getStatus() != 0)
                    return null;
                RoutesBean routesBean = rm.getResult().getRoutes().get(0);
                if (routesBean == null)
                    return null;
                log.info("腾讯地图 DirectionDrivingVO:"+ JSON.toJSONString(rm));
                float distance = Float.parseFloat(String.valueOf(routesBean.getDistance())) / 1000.0F;
                vo.setDistance(distance);
                vo.setSpendTime(routesBean.getDuration());
            } else {
                RouteMatrixVO rm = routeMatrix.getDriving(origins, destinations);
                if (rm == null || rm.getStatus() != 0)
                    return null;

                log.info("百度地图 RouteMatrixVO:"+ JSON.toJSONString(rm));
                vo.setDistance(rm.getDistanceValue());
                vo.setSpendTime(rm.getDurationValue());
            }
            this.writeRouteValue(key, vo);
        }

        return vo;
    }

}
